package com.jinzhi.jzweb.controller;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.jinzhi.jzweb.annotation.OrderLog;
import com.jinzhi.jzweb.domain.JazOrderLogDO;
import com.jinzhi.jzweb.domain.JazProdTypeDO;
import com.jinzhi.jzweb.domain.vo.JazOrderDetailVo;
import com.jinzhi.jzweb.pojo.ChapterItemBankVo;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazOrderDetailDO;
import com.jinzhi.jzweb.service.JazOrderDetailService;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 收支明细表
 * </pre>
 * <small> 2024-07-31 10:00:51 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazOrderDetail")
public class JazOrderDetailController extends BaseController {
	@Autowired
	private JazOrderDetailService jazOrderDetailService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazOrderDetail:jazOrderDetail")
	String JazOrderDetail(){
	    return "jzweb/jazOrderDetail/jazOrderDetail";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:jazOrderDetail:jazOrderDetail")
	public Result<Page<JazOrderDetailDO>> list(JazOrderDetailDO jazOrderDetailDTO){
		// 查询列表数据
		Page<JazOrderDetailDO> page = jazOrderDetailService.selectPage(getPage(JazOrderDetailDO.class),
					jazOrderDetailService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				JazOrderDetailDO obj = (JazOrderDetailDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazOrderDetail:add")
	String add(){
	    return "jzweb/jazOrderDetail/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazOrderDetail:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazOrderDetailDO jazOrderDetail = jazOrderDetailService.selectById(id);
		model.addAttribute("jazOrderDetail", jazOrderDetail);
	    return "jzweb/jazOrderDetail/edit";
	}
	

	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:jazOrderDetail:add")
	public Result<String> save( JazOrderDetailDO jazOrderDetail){
		jazOrderDetailService.insert(jazOrderDetail);
        return Result.ok();
	}
	

	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:jazOrderDetail:edit")
	public Result<String>  update( JazOrderDetailDO jazOrderDetail){
		boolean update = jazOrderDetailService.updateById(jazOrderDetail);
		return update ? Result.ok() : Result.fail();
	}
	

	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazOrderDetail:remove")
	public Result<String>  remove( Long id){
		jazOrderDetailService.deleteById(id);
        return Result.ok();
	}
	

	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:jazOrderDetail:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		jazOrderDetailService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}


	@ResponseBody
	@PostMapping("/saveOrderDetail")
//	@RequiresPermissions("jzweb:jazGroupProd:saveGroupProd")
	public Result<String> saveOrderDetail(@RequestBody JazOrderDetailDO jazOrderDetailDO){
		jazOrderDetailService.saveOrderDetail(jazOrderDetailDO);
		return Result.ok();
	}

	/**
	 *  分页收支明细
	 * @return
	 */
	@GetMapping("/selOrderDetailList")
	@ResponseBody
	public Result<Page<JazOrderDetailDO>> selOrderDetailList(@RequestParam(value = "type",required = false) Integer type,
													   @RequestParam("current")int current,
													   @RequestParam("size")int size,
													   @RequestParam(value = "createTime",required = false) String createTime,
													   @RequestParam(value = "endTime",required = false)String endTime)  {

		return jazOrderDetailService.selOrderDetailList(type,createTime,endTime,current,size);
	}

	/**
	 *  查询总收入，支出
	 * @return
	 */
	@GetMapping("/selOrderDetailMoney")
	@ResponseBody
	public Result<JazOrderDetailDO> selOrderDetailMoney(@RequestParam(value = "type",required = false) Integer type,
														@RequestParam(value = "createTime",required = false) String createTime,
														@RequestParam(value = "endTime",required = false)String endTime)  {

		return jazOrderDetailService.selOrderDetailMoney(type,createTime,endTime);
	}

	/**
	 * 导出操作
	 *
	 * @param response
	 */
	@OrderLog(value = "收支明细导出",sysModule =105,operationType = 4)
	@RequestMapping("/downloadImportTemplate")
	@ResponseBody
	public void downloadImportTemplate(HttpServletResponse response,
									   @RequestParam(value = "type",required = false) Integer type,
									   @RequestParam(value = "createTime",required = false) String createTime,
									   @RequestParam(value = "endTime",required = false)String endTime) {
		List<JazOrderDetailDO> records =jazOrderDetailService.downloadImportTemplate(type,createTime,endTime);
		List<JazOrderDetailVo> voList=new ArrayList<>();
		records.forEach(e->{
			JazOrderDetailVo vo=new JazOrderDetailVo();
			BeanUtils.copyProperties(e,vo);
			if (e.getType()!=null&&e.getType().equals(1)){
				vo.setType("支出");
			}else if (e.getType()!=null&&e.getType().equals(0)){
				vo.setType("收入");
			}
			vo.setUserName(e.getUserName());
			vo.setPhone(e.getPhone());
			DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String  dateStr = sdf.format(e.getTradeTime());
			vo.setTradeTime(dateStr);
			voList.add(vo);
		});

		//导出操作
		EasyPoiUtil.exportExcel(voList, "收入明细表", "收入明细表", JazOrderDetailVo.class, "收入明细表", response);
	}
}
